package com.financial.business.balance.service.yspay.impl;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.financial.business.balance.connector.YsServiceFeign;
import com.financial.business.balance.service.yspay.FinancialBalanceYsPaymentQueryService;
import com.financial.business.balance.utils.JsonUtils;
import com.financial.business.balance.utils.ValidateUtils;
import com.financial.business.balance.utils.apiresult.ApiResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author WANGNING
 * @Email wyn20140226@aliyun.com
 * @Description //银盛余额代付/转账查询
 * @Date 2019/12/17 11:31
 */
@Service
public class FinancialBalanceYsPaymentQueryServiceImpl implements FinancialBalanceYsPaymentQueryService {

    protected static final Logger logger = LoggerFactory.getLogger(FinancialBalanceYsPaymentQueryServiceImpl.class);

    @Autowired
    private YsServiceFeign ysServiceFeign;

    @Override
    public ApiResult paymentQuery(String requestParam) {
        logger.info("FinancialBalanceYsPaymentQueryServiceImpl.paymentQuery---接收优店积分代付查询参数json" + requestParam);
        Map<String, Object> params = JsonUtils.jsonToMap(requestParam);
        /*三方支付专用的交易单号*/
        Object tempParam = params.get("out_trade_no");
        String outTradeNo = tempParam == null ? null : tempParam.toString();
        if (StringUtils.isBlank(outTradeNo)) {
            /*对所传字段进行非空校验判断*/
            return ApiResult.failure(ValidateUtils.validateProperty(requestParam));
        }
        Map<String, Object> paraMap = new HashMap<>();
        paraMap.put("out_trade_no", outTradeNo);
        String paramJson = JsonUtils.toJSon(paraMap);
        try {
            ApiResult result = ysServiceFeign.refundQuery(paramJson);
            logger.info("FinancialBalanceYsPaymentQueryServiceImpl.paymentQuery---调用优店余额主动查询返回信息" + result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("FinancialBalanceYsPaymentQueryServiceImpl.paymentQuery---查询优店余额预支付信息失败!" + e.getMessage());
        }
        return ApiResult.failure();
    }
}
